﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.IO;

namespace DMS.DocumentStoreUpdater
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class DocumentStoreUpdater : SPItemEventReceiver
    {

       public override void ItemAdded(SPItemEventProperties properties)
       {
           base.ItemAdded(properties);
           SPListItem item = properties.ListItem;
           item["Title"] = item["LinkFilename"];

           this.EventFiringEnabled = false;
           item.UpdateOverwriteVersion();
           this.EventFiringEnabled = true;
       }

       /// <summary>
       /// An item is being updated.
       /// </summary>
       public override void ItemUpdating(SPItemEventProperties properties)
       {
           base.ItemUpdating(properties);

           
       }

       public override void ItemUpdated(SPItemEventProperties properties)
       {
           base.ItemUpdated(properties);
           SPListItem item = properties.ListItem;
           string docType = item["DocumentType"].ToString();

           try
           {
               if (docType == "GAT" || docType == "Credit Applications")
               {

                   SPFile sourceFile = properties.ListItem.File;
                   SPFile destFile;

                   using (Stream stream = sourceFile.OpenBinaryStream())
                   {
                       SPDocumentLibrary destLibrary;
                       if (docType == "GAT")
                       {
                           destLibrary = (SPDocumentLibrary)properties.ListItem.Web.Webs["GATs"].Lists["GATs"];
                       }
                       else
                       {
                           destLibrary = (SPDocumentLibrary)properties.ListItem.Web.Webs["CreditApplications"].Lists["CreditApplications"];
                       }
                       destFile = destLibrary.RootFolder.Files.Add(sourceFile.Name, stream);
                       stream.Close();

                   }

                   SPListItem destItem = destFile.Item;
                   SPListItem sourceItem = sourceFile.Item;
                   destItem["Title"] = sourceItem["Title"];
                   destItem["DocumentType"] = sourceItem["DocumentType"];
                   destItem["DocumentDate"] = sourceItem["DocumentDate"];
                   destItem["CustomerName"] = sourceItem["CustomerName"];
                   destItem["CustomerNumber"] = sourceItem["CustomerNumber"];
                   this.EventFiringEnabled = false;
                   destItem.UpdateOverwriteVersion();
                   sourceFile.Item.Delete();
                   this.EventFiringEnabled = true;
               }
           }
           catch (Exception ex)
           {

               throw ex;
           }

       }

    }
}
